package com.smile.chatview;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import com.google.gson.Gson;
import com.smile.model.LoginMessage;
import com.smile.model.SendString;

public class test extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private String sendText;
	private JTextArea textArea;
	private JButton btnSend;
	private Socket socket;
	private BufferedReader reader;
	private PrintWriter writer;
	private JButton btnConn;
	private JTextArea textArea_1;
	private JPanel panel;
	private JScrollPane scrollPane;
	private JScrollPane scrollPane_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					test frame = new test();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public test() {
		setAlwaysOnTop(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		addWindowListener(new WindowAdapter() {

			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
				// System.out.println("关闭");
				try {
					if (writer != null) {
						writer.close();
					}
					if (reader != null) {
						reader.close();
					}
					if (socket != null && !socket.isClosed()) {
						socket.close();
					}
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}

		});

		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(0, 200, 434, 61);
		contentPane.add(scrollPane_1);
		
				textArea = new JTextArea();
				scrollPane_1.setViewportView(textArea);
		
		panel = new JPanel();
		panel.setBounds(0, 0, 434, 36);
		contentPane.add(panel);
		panel.setLayout(null);
		
				btnSend = new JButton("send");
				btnSend.setBounds(219, 0, 215, 36);
				panel.add(btnSend);
				
						btnConn = new JButton("connect");
						btnConn.setBounds(0, 0, 220, 36);
						panel.add(btnConn);
						btnConn.addMouseListener(new MouseAdapter() {
							@Override
							public void mouseClicked(MouseEvent e) {

								new Thread() {
									@Override
									public void run() {
										try {
											socket = new Socket("192.168.31.151", 8888);
											writer = new PrintWriter(new OutputStreamWriter(
													socket.getOutputStream(), "utf-8"));

											reader = new BufferedReader(new InputStreamReader(
													socket.getInputStream(), "utf-8"));

											String line = "";
											while ((line = reader.readLine()) != null) {
												textArea_1.append("-------" + line
														+ "--------\n");
											}
											writer.close();
											reader.close();
											writer = null;
											reader = null;

										} catch (UnknownHostException e) {
//											System.out.println(e.getMessage());
											textArea_1.append(e.getLocalizedMessage() + "\n");
										} catch (IOException e) {
//											System.out.println(e.getMessage());
											textArea_1.append(e.getLocalizedMessage() + "\n");
										}
									};
								}.start();
							}
						});
				btnSend.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {

						if (writer != null) {
							sendText = textArea.getText();
							if (sendText.equals("123")) {
//								System.out.println(sendText.indexOf("\r\n") + "-----" + sendText);
								writer.write(sendText + "\n\r\n");
								writer.flush();
								
								textArea_1.append("我说：" + sendText + "\n");
							} else {
								LoginMessage loginMessage = new LoginMessage("123456", sendText);
								String json = new Gson().toJson(loginMessage);
								SendString sendString = new SendString(2, json);
								String sendJson = new Gson().toJson(sendString);
								
								writer.write(sendJson + "\n");
								writer.flush();
								textArea_1.append("已发送登录信息\n");
							}
						} else {
							textArea_1.append("与服务器断开连接，无法发送\n");
						}
					}
				});
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 35, 434, 144);
		contentPane.add(scrollPane);
		
				textArea_1 = new JTextArea();
				textArea_1.setEditable(false);
				scrollPane.setViewportView(textArea_1);
				textArea_1.setLineWrap(true);
				textArea_1.setWrapStyleWord(true);
	}
}
